﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Chromos.Actors;

namespace Chromos.ActionEngine
{
	public class AttackAction : VisualNonLoopingAction
	{
        public enum ProjectileType
        {
            None,
            Bullet
        }

		/// <summary>
		/// Damage of the attack (currently ignoring defence)
		/// </summary>
		public int damage;
		/// <summary>
		/// Use of the actor's energy when he attacks
		/// </summary>
		public int energyUse;
		/// <summary>
		/// Max range (AOE) of this attack
		/// </summary>
		public int range; 
		/// <summary>
		/// Number of milliseconds delay before this attack inflicts damage and energy again (or can be used again)
		/// </summary>
		public int rate; 
		/// <summary>
		/// Time limit on the use of this attack
		/// </summary>
		public int time; 
		/// <summary>
		/// Absolute rarity of this attack when close, in percentage
		/// </summary>
		public int closeFreq;
		/// <summary>
		/// Absolute rarity of this attack when far away, in percentage
		/// </summary>
		public int longFreq;
		public FACING facing;

		/// <summary>
		/// Whether the attack is finished through time or through the animation
		/// </summary>
		public bool timeFinish = false;

		public int lastAttacked = 0;

		private Func<int, Projectile> projectileFunc;

		public AttackAction(Actor actor, ContentManager content, Vector2 force, ForceType forceType, int animDelay, Func<int, Projectile> projectileFunc, int damage, int energyUse, int range, int rate, int time, int closeFreq, int longFreq, List<Actor> targets, List<Projectile> projectiles, FACING facing, bool timeFinish)
			: base(actor, content, force, forceType, animDelay)
		{
			//int animDelay, int damage, int energyUse, int range, 
			//int rate, int time, int closeFreq, int longFreq)
			this.animDelay = animDelay;
			this.damage = damage;
			this.energyUse = energyUse;
			this.range = range;
			this.rate = rate;
			this.time = time;
			this.closeFreq = closeFreq;
			this.longFreq = longFreq;
			this.projectileFunc = projectileFunc;
			this.facing = facing;
			this.timeFinish = timeFinish;
		}

		public void start(int i)
		{
			if (actor is Player)
			{
				foreach (Enemy e in actor.game.enemies)
				{
					if (Math.Sqrt((actor.position.X - e.position.X) * (actor.position.X - e.position.X) + (actor.position.Y - e.position.Y) * (actor.position.Y - e.position.Y)) <= this.range)
						e.health -= this.damage;
				}
			}

			if (projectileFunc == null)
			{
				base.start();
				return;
			}

			// Initalize projectiles
			List<Projectile> projectiles = new List<Projectile>();

			Projectile p = projectileFunc(i);

			if(p != null){
				projectiles.Add(p);
				this.actor.game.projectiles.AddRange(projectiles);
			}

			base.start();
		}

		public override bool isFinished()
		{
			if (this.timeFinish)
				return lastAttacked > 100;
			else
				return base.isFinished();
		}
	}
}
